package com.gzsxy.sso.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzsxy.sso.common.dto.request.FunctionRequest;
import com.gzsxy.sso.common.result.R;
import com.gzsxy.sso.core.entity.Function;
import com.gzsxy.sso.core.entity.vo.FunctionQueryVo;
import com.gzsxy.sso.core.service.FunctionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 功能管理
 * @date 2021/11/18 11:22
 */
@Api(description = "功能管理")
@CrossOrigin  //解决跨域
@RestController
@RequestMapping("/admin/function")
public class FunctionController {

    @Autowired
    private FunctionService functionService;


    @ApiOperation("查询功能列表")
    @GetMapping("/getAll")
    public R list(){
        List<Function> list = functionService.list();
        return R.ok().message("查询成功").data("data",list);
    }

    @ApiOperation("分页功能列表查询")
    @GetMapping("/list")
    public R getAllFunction(@ApiParam(value = "当前页面",required = true,defaultValue ="1" ) @RequestParam Long page,         //long数据类型适用于较大整数类型
                            @ApiParam(value = "每页记录数",required = true,defaultValue ="5") @RequestParam Long limit,
                            @ApiParam("查询对象") FunctionQueryVo functionQueryVo){
        Page<Function> pageParam = new Page<>(page, limit);
        IPage<Function> list = functionService.getAllFunction(pageParam,functionQueryVo);
        //获取查询的总结果
        List<Function> records = list.getRecords();
        //总记录数
        long total = list.getTotal();
        //之所以ok（）方法后面还可以加方法是因为ok方法返回是r对象，对象又可以调用他的方法
        return R.ok().message("查询成功").data("total",total).data("data",records);
    }


    @ApiOperation("添加功能")
    @PostMapping("/add")
    public R addFunction(@RequestBody FunctionRequest functionRequest){
        functionService.addFunction(functionRequest);
        return R.ok().message("添加成功");
    }



    @ApiOperation("根据id查询功能")
    @GetMapping("/{functionId}")
    public R selectById(@ApiParam(value = "功能id",required = true)
                            @PathVariable String functionId){
        Function function = functionService.selectById(functionId);
        return R.ok().message("查询成功").data("data",function);
    }


    @ApiOperation("编辑功能")
    @PutMapping("update")
    public R updateFunction(@RequestBody FunctionRequest functionRequest){
        functionService.updateFunction(functionRequest);
        return R.ok().message("编辑成功");
    }


    @ApiOperation("删除功能")
    @DeleteMapping("/delete/{functionId}")
    public R delete(@ApiParam(value = "功能id",required = true)@PathVariable String functionId,
                    @ApiParam(value = "是否确认操作",required = true)@RequestParam Boolean isConform){
        functionService.delete(functionId,isConform);
        return R.ok().message("删除成功");
    }


    @ApiOperation("根据appId查询当前下的功能列表")
    @GetMapping("/getFunByAppId")
    public R selectFunByAppId(@ApiParam(value = "appId") @RequestParam String appId){
        List<Function> list = functionService.selectFunByAppId(appId);
        return R.ok().message("查询成功").data("data",list);
    }

}
